﻿/**
//  SetPropertyCommand.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/
using System.Runtime.InteropServices;
using EDSDKLib;
using MarqueeMoment.CameraController.Camera;

namespace MarqueeMoment.CameraController.Command
{
    public class SetPropertyCommand : Command
    {

        private uint propertyID;

        private uint data;

        public SetPropertyCommand(CameraModel model, uint propertyID, uint data)
            : base(model)
        {
            this.propertyID = propertyID;
            this.data = data;
        }

        //// Execute a command.	
        public override bool execute()
        {

            uint err = EDSDK.EDS_ERR_OK;
            bool locked = false;

            //// You should do UILock when you send a command to camera models elder than EOS30D.

            if (base.model.isLegacy())
            {
                err = EDSDK.EdsSendStatusCommand(base.model.getCameraObject(), EDSDK.CameraState_UILock, 0);


                if (err == EDSDK.EDS_ERR_OK)
                {
                    locked = true;

                }
            }

            //// Stock the property.

            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsSetPropertyData(base.model.getCameraObject(), this.propertyID, 0, Marshal.SizeOf(this.data), this.data);

            }


            if (locked)
            {
                err = EDSDK.EdsSendStatusCommand(base.model.getCameraObject(), EDSDK.CameraState_UIUnLock, 0);

            }

            //// Notify Error.
            if (err != EDSDK.EDS_ERR_OK)
            {
                //// Retry when the camera replys deviceBusy.

                if (err == EDSDK.EDS_ERR_DEVICE_BUSY)
                {
                    base.model.notifyObservers(commandName.warn, err);

                    return false;
                }

                base.model.notifyObservers(commandName.errr, err);

            }

            return true;

        }

    }
}
